<template>
  <e-table
    ref="eTable"
    @getList="handleGetList"
    :tableData="tableData"
    :columns="columns"
    :loading="loading"
    :border="false"
    :pageObj="null"
    :rowKey="'evalTypeId'"
    :treeProps="{children: 'children', hasChildren: 'hasChildren'}"
  >
    <el-table-column slot="sort" label="排序" width="100" align="center">
      <template slot-scope="scope">
        <el-input
          style="width: 65px"
          :max="100"
          size="mini"
          v-model.number="scope.row.evalTypeOrder"
          @blur="handleOrderChange(scope.row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="项目数" slot="num" align="center" width="80">
      <template slot-scope="scope">{{ scope.row.evalItemList.length }}</template>
    </el-table-column>
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item
              v-if="$auth('eval:update')"
              @click.native="handleEdit(true, scope.row)"
            >
              <a>编辑</a>
            </el-dropdown-item>
            <el-dropdown-item v-if="$auth('eval:delete')" @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
import { getAll, typeDelete, classifyList } from '@/api/eval'
import { indexMethod, orderChange, openNotification } from '@/utils/util'

export default {
  data() {
    return {
      queryParam: {
        pageNo: 1,
        pageSize: 10
      },
      loading: false,
      // 表头
      columns: [
        {
          label: '考评项目分类名称',
          prop: 'evalTypeName',
          align: 'left'
        },
        {
          slot: 'num'
        },
        {
          label: '管理人',
          align: 'center',
          width: '100px',
          prop: 'managerName'
        },
        { slot: 'sort' },
        { slot: 'action' }
      ],
      tableData: []
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    indexMethod,
    async handleOrderChange(row) {
      const res = await orderChange(row, 'evalTypeOrder', 'evalTypeId', 'type', '/eval/')
      if (res.state === '000000') {
        this.handleGetList()
      }
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.evalTypeName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await typeDelete(row.evalTypeId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    handleEdit(bool, row) {
      this.$emit('edit', bool, row)
    },
    handleData(arr1, arr2) {
      arr1.map(v => {
        v.children = []
        arr2.map(j => {
          if (v.evalTypeId === j.parentId) {
            v.children.push(j)
            if (v.children && v.children.length > 0) {
              this.handleData(v.children, arr2)
            }
          }
        })
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      const res = await classifyList(param)
      if (res && res.data.length > 0) {
        const { state, data } = res
        let parentArr = []
        data.map(v => {
          if (!v.parentId) {
            parentArr.push(v)
          }
        })
        this.handleData(parentArr, data)
        this.tableData = parentArr
        // console.log('tableData', this.tableData)
      } else {
        this.tableData = []
      }
      this.loading = false
    }
  }
}
</script>
